/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Params;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.net.Socket;

/**
 *
 * @author Administrator
 */
public class ThreadConnectionSocketSpyScreen extends Thread implements Runnable{
     public SocketListener socketListener;
    public Socket connectionSocket;
    public ObjectInputStream inFromClient;//nhap du lieu tu client goi
    public ObjectOutputStream outToClient;//xuat du lieu den client 
    public BufferedReader bufferedReader;//doc chuoi tu mang goi ve
    public PrintWriter bufferedWriter;//ghi chuoi xuat len mang

    public ThreadConnectionSocketSpyScreen(Socket socket, SocketListener socketListener) {
        this.connectionSocket = socket;
        this.socketListener = socketListener;
        try {
            this.bufferedReader = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            this.bufferedWriter = new PrintWriter(connectionSocket.getOutputStream(), true);
            outToClient  = new ObjectOutputStream(connectionSocket.getOutputStream());

        } catch (Exception e) {
            System.out.println(e);
        }
        start();//goi thread chay
    }

    @Override
    public void run() {
        while (true) {
            try {
                
                ChatMessage message = new ChatMessage();
                message.setType(4);
                message.setName(String.valueOf(this.connectionSocket.getPort()));
                message.setMessage(this.bufferedReader.readLine().toString());
                socketListener.onRead(message);
            } catch (Exception e) {
                System.out.println(e);
            }
            //lenh nay thong bao cho scheduler biet(dag ranh) de thuc thi 1 thread khac
            Thread.yield();
        }
    }
    
}
